{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import bqplot.pyplot as plt\n",
    "import string"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Basic Pie Chart"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "scrolled": false
   },
   "outputs": [],
   "source": [
    "data = np.random.rand(3)\n",
    "\n",
    "fig = plt.figure(animation_duration=1000)\n",
    "pie = plt.pie(data, display_labels='outside', labels=list(string.ascii_uppercase))\n",
    "fig"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Update Data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "n = np.random.randint(1, 10)\n",
    "pie.sizes = np.random.rand(n)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Display Values"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "with pie.hold_sync():\n",
    "    pie.display_values = True\n",
    "    pie.values_format = '.1f'"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Enable sort "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "pie.sort = True"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Set different styles for selected slices"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "pie.selected_style = {'opacity': 1, 'stroke': 'white', 'stroke-width': 2}\n",
    "pie.unselected_style = {'opacity': 0.2}\n",
    "pie.selected = [1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "pie.selected = None"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "For more on piechart interactions, see the [Mark Interactions notebook](../Interactions/Mark Interactions.ipynb)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Modify label styling"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "pie.label_color = 'Red'\n",
    "pie.font_size = '20px'\n",
    "pie.font_weight = 'bold'"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Update pie shape and style"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "scrolled": false
   },
   "outputs": [],
   "source": [
    "fig1 = plt.figure(animation_duration=1000)\n",
    "pie1 = plt.pie(np.random.rand(6), inner_radius=0.05)\n",
    "fig1"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Change pie dimensions"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "# As of now, the radius sizes are absolute, in pixels\n",
    "with pie1.hold_sync():\n",
    "    pie1.radius = 150\n",
    "    pie1.inner_radius = 100"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "# Angles are in radians, 0 being the top vertical\n",
    "with pie1.hold_sync():\n",
    "    pie1.start_angle = -90\n",
    "    pie1.end_angle = 90"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Move the pie around\n",
    "`x` and `y` attributes control the position of the pie in the figure.\n",
    "If no scales are passed for `x` and `y`, they are taken in absolute \n",
    "figure coordinates, between 0 and 1."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "pie1.y = 0.1\n",
    "pie1.x = 0.6\n",
    "pie1.radius = 180"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Change slice styles\n",
    "Pie slice colors cycle through the `colors` and `opacities` attribute, as the `Lines` Mark."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "scrolled": false
   },
   "outputs": [],
   "source": [
    "pie1.stroke = 'brown'\n",
    "pie1.colors = ['orange', 'darkviolet']\n",
    "pie1.opacities = [.1, 1]\n",
    "fig1"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Represent an additional dimension using Color"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The `Pie` allows for its colors to be determined by data, that is passed to the `color` attribute. \n",
    "A `ColorScale` with the desired color scheme must also be passed."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "from bqplot import ColorScale, ColorAxis\n",
    "\n",
    "n = 7\n",
    "size_data = np.random.rand(n)\n",
    "color_data = np.random.randn(n)\n",
    "\n",
    "fig2 = plt.figure()\n",
    "plt.scales(scales={'color': ColorScale(scheme='Reds')})\n",
    "pie2 = plt.pie(size_data, color=color_data)\n",
    "fig2"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 1
}
